<template>
  <!-- 一条文章单元格 -->
  <div>
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{ obj.title }}</span>
          <img
            class="thumb"
            :src="obj.cover.images[0]"
            v-if="obj.cover.type === 1"
          />
        </div>
        <div class="thumb-box" v-if="obj.cover.type > 1">
          <img
            class="thumb"
            v-for="(imgUrl, index) in obj.cover.images"
            :key="index"
            :src="imgUrl"
          />
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <span>{{ obj.aut_name }}</span>
            <span>{{ obj.comm_count }}评论</span>
            <span>{{ formatTime(obj.pubdate) }}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" v-if="showCross" @click.stop="show = true" />
          <!-- 一级反馈面板 -->
          <van-action-sheet
            v-model="show"
            :actions="actions"
            cancel-text="取消"
            get-container="body"
            @select="onSelect"
          />
          <!-- 二级反馈面板 -->
          <van-action-sheet
            v-model="show2"
            :actions="actions2"
            cancel-text="返回"
            close-on-click-action
            get-container="body"
            @select="onSelect"
          />
          <van-dialog
            v-model="show3"
            title="反馈问题"
            get-container="body"
            show-cancel-button
            :before-close="befClose"
          >
            <van-form>
              <van-field
              v-focus
                type="textarea"
                v-model="message"
                placeholder="请输入问题"
                border
                :rules="[{required:true}]"
              >
              </van-field>
            </van-form>
          </van-dialog>
        </div>
      </template>
    </van-cell>
  </div>
</template>

<script>
import { FIRST_ACTION_SHEET, SECOND_ACTION_SHEET } from '@/constant'
import moment from 'moment'
import { articleDislikeAPI, articleReportsAPI } from '@/api/home.js'
import { Toast } from 'vant'
export default {
  data () {
    return {
      show: false,
      show2: false,
      show3: false,
      message: '',
      actions: FIRST_ACTION_SHEET,
      actions2: SECOND_ACTION_SHEET
    }
  },
  props: {
    obj: {
      type: Object,
      required: true
    },
    i: Number,
    showCross: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    formatTime (time) {
      moment.locale('zh-cn')
      return moment(time).from(new Date())
    },
    onCancel () {},
    async onSelect (obj) {
      if (obj.id === 1) {
        this.show2 = true
      } else if (obj.id === 2) {
        this.$emit('del-art', this.i)
        try {
          await articleDislikeAPI({
            target: this.obj.art_id
          })
          Toast('反馈成功')
        } catch (err) {
          Toast('反馈失败')
        }
      } else if (obj.id === 3) {
      } else {
        if (obj.id === 0) {
          this.show3 = true
          this.show2 = this.show = false
        } else {
          await articleReportsAPI({
            target: this.obj.art_id,
            type: obj.id + '',
            remark: obj.name
          })
          Toast('反馈成功')
        }
      }
    },
    // 关闭前的回调函数，调用 done() 后关闭弹窗，调用 done(false) 阻止弹窗关闭
    async befClose (action, done) {
      if (action === 'confirm') {
        if (this.message.length === 0) {
          Toast('请输入内容')
          done(false)
        } else {
          done()
          try {
            await articleReportsAPI({
              target: this.obj.art_id,
              type: 0,
              remark: this.message
            })
            Toast('反馈成功')
          } catch (err) {
            Toast('反馈失败')
          }
          this.message = ''
        }
      } else {
        done()
      }
    }
  }
}
</script>

<style scoped lang="less">
/* 标题样式 */
.title-box {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
}

/* label描述样式 */
.label-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

/* 文章信息span */
.label-box span {
  margin: 0 3px;
  &:first-child {
    margin-left: 0;
  }
}
/* 图片的样式, 矩形黄金比例：0.618 */
.thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
}

/* 三图, 图片容器 */
.thumb-box {
  display: flex;
  justify-content: space-between;
}
</style>
